package com.my.study.io.writer;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

/**
 * @author Carlos
 * @version 1.0
 * @Description 写入处理流
 * @date 2021/6/27 8:43
 **/
@Slf4j(topic = "c.BufferedWriterTest")
public class BufferedWriterTest {

    public static void main(String[] args) {
        String path = "D:/file/ok.txt";
        test02(path);
    }

    private static void test02(String path) {
        BufferedWriter writer = null;

        try {
            writer = new BufferedWriter(new FileWriter(path));

            writer.write("OKKKK!");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void test01(String path) {
        try (BufferedWriter writer = new BufferedWriter(
                // 追加还有在节点流进行设置
                new FileWriter(path, true))) {
            writer.write("OK 1!");
            writer.newLine();

            writer.write("OK 2!");
            writer.newLine();

            writer.write("OK 3!");
            writer.newLine();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
